//
//  SearchViewController.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "CarCategory.h"
#import "BrandFindTableViewCell.h"
#import "DetailCarCategory.h"
#import "DtailBrandTableViewCell.h"
#import "DetailInfoViewController.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)NSMutableArray *arrSmall;
@property (nonatomic, retain)UITableView *tableViewSmall;
@property (nonatomic, retain)UIView *resultView;
@property (nonatomic, retain)UISegmentedControl *segmentedControl;


//条件筛选
@property (nonatomic, retain)UITableView *CDTableView;
@property (nonatomic, retain)NSMutableArray *CDArray;
//刷新
@property(nonatomic, retain)HaveAEasyHUD *progressHud;
@end

@implementation SearchViewController

- (void)dealloc
{
    [_tableView release];
    [_tableViewSmall release];
    [_arr release];
    [_arrSmall release];
//    [_scrollView release];
    
    //条件筛选
    [_CDTableView release];
    [_CDArray release];
    [super dealloc];
    
}


//处理品牌找车本地数据
- (void)handleData
{
    //品牌找车界面数据处理
    self.arr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    NSMutableArray *array = [[result objectForKey:@"result"]objectForKey:@"brandlist"];
    for (NSDictionary *dic in array) {
        CarCategory *info = [[CarCategory alloc]initWithDictionary:dic];
        [self.arr addObject:info];
       
        [info release];
  
    }
 
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"品牌找车";
    
    
    
//    - (instancetype)initOneProgressHud:(UIView*)view;
//    - (void)hudWasHidden:(MBProgressHUD *)hud;
    
    
    
    //创建tabView

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate =self;
    [self.view addSubview:self.tableView];
    [_tableView release];

    //设置视图样式,刚开始为隐藏状态，点击cell时候出现
    self.resultView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/6, 64, self.view.frame.size.width/6*5, self.view.frame.size.height-113)];
    self.resultView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.resultView];
    self.resultView.hidden = YES;
  
    [_resultView release];
    
   

    
    //view上添加"在售"按钮
    UIButton *onsale = [UIButton buttonWithType:UIButtonTypeSystem];
    onsale.frame = CGRectMake(self.resultView.frame.size.width/4, 10, self.resultView.frame.size.width/2, self.resultView.frame.size.height/15);
    onsale.backgroundColor = [UIColor colorFromHexCode:@"#224b8f"];
    [onsale setTitle:@"可售" forState:UIControlStateNormal];
    onsale.layer.cornerRadius = 10;
    onsale.tintColor = [UIColor whiteColor];
    [self.resultView addSubview:onsale];
    
    //view上添加uitableview
    self.tableViewSmall = [[UITableView alloc]initWithFrame:CGRectMake(0, self.resultView.frame.size.height/10, self.resultView.frame.size.width, self.resultView.frame.size.height/10*9)];
//    self.tableViewSmall.backgroundColor = [UIColor blueColor];
    self.tableViewSmall.dataSource = self;
    self.tableViewSmall.delegate = self;
 
    [self.resultView addSubview:self.tableViewSmall];
    [_tableViewSmall release];

    //给视图添加轻扫手势，轻扫时消失
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.resultView addGestureRecognizer:swipe];
    [swipe release];
    
    //加载数据
    [self handleData];
}




//每个分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_tableView == tableView) {
        CarCategory *info = [self.arr objectAtIndex:section];
        NSArray *array = [info valueForKey:@"list"];
        return  array.count;
    }
    DetailCarCategory *info2 = [self.arrSmall objectAtIndex:section];
    NSArray *array2 = [info2 valueForKey:@"serieslist"];
    return array2.count;
  
}


//cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        
    
    static NSString *str = @"ff";
    BrandFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        
        cell = [[[BrandFindTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    
    //传值
    CarCategory *info = [self.arr objectAtIndex:indexPath.section];
    NSDictionary *dic = [info.list objectAtIndex:indexPath.row];
    cell.info = dic;
    return cell;
}
 
    static NSString *str2 = @"tt";
    DtailBrandTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:str2];
    if (cell2 == nil) {
        
        cell2 = [[[DtailBrandTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str2]autorelease];;
    }
    
    //传值
    DetailCarCategory *info2 = [self.arrSmall objectAtIndex:indexPath.section];
    NSDictionary *dic2 = [info2.serieslist objectAtIndex:indexPath.row];
    cell2.info2 = dic2;
    return cell2;
}


//设置分区数量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //判断是哪个tableview
    if (_tableView == tableView) {
        return self.arr.count;
    } return self.arrSmall.count;
    
}

//设置侧边栏各区名称
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //判断是哪个tableview
    if (_tableView == tableView) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger i = 0; i < self.arr.count; i++) {
            CarCategory *carinfo = [self.arr objectAtIndex:i];
            NSString *str = carinfo.letter;
            //        NSLog(@"%@",str);
            [array addObject:str];
            
        }
        return array;
    }return nil;
   
}

//设置分区顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //判断是哪个tableview
    if (_tableView == tableView) {
        return 30;
    }return 40;
    
}

//设置各个分区顶部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //判断是哪个tableview
    if (_tableView == tableView) {
        CarCategory *info = [self.arr objectAtIndex:section];
        NSString *str = info.letter;
        return str;

    }DetailCarCategory *info = [self.arrSmall objectAtIndex:section];
    NSString *str = [info valueForKey:@"name"];
    return str;
    
}

//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        return 60;
    }return 70;
}

//点击cell弹出一个视图

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断是哪个tableview
    if (_tableView == tableView) {
        
        
        //点击——tableview的cell时，self.resultView取消隐藏
        self.resultView.hidden = NO;
        
        //初始化菊花
    self.progressHud = [[HaveAEasyHUD alloc] initOneProgressHud:self.view];
        
//    传递所在cell网址数据
    CarCategory *info = [self.arr objectAtIndex:indexPath.section];
    NSNumber *number = [[info.list objectAtIndex:indexPath.row]valueForKey:@"id"];
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/cars/seriesprice-a2-pm2-v4.6.1-b%@-t1.html",number];
    NSLog(@"%@",str);
    
    //处理数据
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"成功%@",responseObject);
        NSError *error = nil;
        NSJSONSerialization *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *array = [[result valueForKey:@"result"]valueForKey:@"fctlist"];
//        NSLog(@"%@",array);
        self.arrSmall = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            DetailCarCategory *carInfo = [[DetailCarCategory alloc]initWithDictionary:dic];
            [self.arrSmall addObject:carInfo];
            
            [carInfo release];
        }
        
        //重新加载数据
        [self.tableViewSmall reloadData];
        
        //结束菊花刷新状态
        [self.progressHud hudWasHidden:self.progressHud.progressHud];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@",error);
    }];
    
     }
    
    //点击小tabview的cell进入详情界面
    else if (_tableViewSmall == tableView){
    DetailInfoViewController *detailInfoVC = [[DetailInfoViewController alloc]init];
        
        DetailCarCategory *info = [self.arrSmall objectAtIndex:indexPath.section];
        NSDictionary *dic = [info.serieslist objectAtIndex:indexPath.row];
        NSNumber *number = [dic valueForKey:@"id"];
        detailInfoVC.CarId = number;
        
        
        
    [self.navigationController pushViewController:detailInfoVC animated:YES];
//    [detailInfoVC release];
    }
    
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    UIView *view = swipe.view;
    view.hidden = YES;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
